/**
 * Copyright (C) 2009+ Spark Zheng
 *
 * @file        include/buffer.h 
 * @date        2010-04-05
 * @brief
 * @see         dlib
 * @history
 */

#ifndef _DLIB_BUFFER_H
#define _DLIB_BUFFER_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include "global.h"

typedef struct buffer_struct dlib_buffer_t;

struct buffer_struct {
	size_t	buff_size;	/** buffer space-size */
	size_t	buff_offset;	/** buffer pointer position */
	char	buffer[];	/** buffer space */
};

/**
 * dlib_buffer_init
 * @brief init a buffer
 * @return buffer or NULL
 * @param size: preallocated buffer size
 */
extern dlib_buffer_t *dlib_buffer_init(size_t size);

/**
 * dlib_buffer_set_offset
 * @brief set the offset of a buffer, Caution to use it
 * @param buffer: the buffer
 * @param offset: set value
 */
extern dlib_buffer_t *dlib_buffer_set_offset(dlib_buffer_t *buffer, size_t offset);

/**
 * dlib_buffer_append
 * @brief append a string to a buffer
 * @param buffer: the buffer
 * @param append: string to be appended
 * @param len: string length
 */
extern dlib_buffer_t *dlib_buffer_append(dlib_buffer_t *buffer, const char *append, size_t len);

/**
 * dlib_buffer_prepend
 * @brief prepend a string to a buffer
 * @param buffer: the buffer
 * @param prepend: string to be prepended
 * @param len: string length
 */
extern dlib_buffer_t *dlib_buffer_prepend(dlib_buffer_t *buffer, const char *prepend, size_t len);

/**
 * dlib_buffer_exit
 * @brief destroy a buffer
 * @param buffer: the buffer
 */
extern void dlib_buffer_exit(dlib_buffer_t *buffer);

/**
 * dlib_buffer_dump
 * @brief dump out a buffer
 * @param buffer: the buffer
 * @param fp: dump out file-struct
 */
extern void dlib_buffer_dump(dlib_buffer_t *buffer, FILE *fp);

#ifdef __cplusplus
}
#endif
#endif
